import resources.RegistNetPropertyBundle;
import registnet.dao.RegistrarUserPeer;
import registnet.dao.CollegeUserPeer;
import registnet.dao.UnitUserPeer;
import registnet.dao.CollegePeer;
import registnet.dao.UnitPeer;

/* [start] 	initializes gui variables */
RegistNetPropertyBundle bundle = RegistNetPropertyBundle.getInstance("/resources/registnet");

String _logo =  bundle.getProperty("registnet.logo");

String _sign_in = bundle.getProperty("registnet.registration.toolbar.sign_in");
String _authors = bundle.getProperty("registnet.registration.toolbar.authors");
String _help = bundle.getProperty("registnet.registration.toolbar.help");

String _user_type = bundle.getProperty("registnet.registration.form.user_type"); 
String _college = bundle.getProperty("registnet.registration.form.college");
String _unit = bundle.getProperty("registnet.registration.form.unit");
String _username = bundle.getProperty("registnet.registration.form.username"); 
String _password = bundle.getProperty("registnet.registration.form.password"); 
String _repassword = bundle.getProperty("registnet.registration.form.repassword");
String _register = bundle.getProperty("registnet.registration.form.register");
String _search = bundle.getProperty("registnet.registration.form.search");

String _administrator = bundle.getProperty("registnet.registration.form.user.administrator");
String _registrar = bundle.getProperty("registnet.registration.form.user.registrar");
String _college = bundle.getProperty("registnet.registration.form.user.college");
String _unit = bundle.getProperty("registnet.registration.form.user.unit");

List data = new Vector();
List colleges = new Vector();
List units = new Vector(); 
/* [end] */



/* [start] 	defines the renderer for a listbox */
public class MyItemRenderer implements ListitemRenderer {
	public void render(Listitem item, java.lang.Object data) {
		new Listcell(data).setParent(item);
	}
}
/* [end] */



/* [start] 	retrieves the college codes and places each of them as an option for the
			user who wishes to create a college user account */
void retrieveColleges(){
	data.clear();			  
	String[] listModel = new String[colleges.size()];
	for (int i=0; i<colleges.size(); i++){
		listModel[i] = colleges.get(i).getCollegeCode();
		data.add(listModel[i]);
	}
	ListModel strset = new ListModelList(listModel);
	office_lb.setModel(strset);
	office_lb.setItemRenderer(new MyItemRenderer());
}
/* [end] */



/* [start] 	retrieves the unit codes and places each of them as an option for the
			user who wishes to create a unit user account */
void retrieveUnits(){											  
	String[] listModel = new String[units.size()];
	data.clear();
	for (int i=0; i<units.size(); i++){
		listModel[i] = units.get(i).getUnitCode();
		data.add(listModel[i]);
	}
	ListModel strset = new ListModelList(listModel);
	office_lb.setModel(strset);
	office_lb.setItemRenderer(new MyItemRenderer());
}



/* [start]	updates the registration form after the user selects the type of account */
void updateForm(){
	if (user_type.value.equals(_college)){
		office_l.setVisible(true);
		office.setVisible(true);
		office_s.setVisible(true);
		office_l.setValue(_college);
		colleges = new CollegePeer().getAllColleges();
		retrieveColleges();
	}
	else if (user_type.value.equals(_unit)){
		office_l.setVisible(true);
		office.setVisible(true);
		office_s.setVisible(true);
		office_l.setValue(_unit);
		units = new UnitPeer().getAllUnits();
		retrieveUnits();
	}
	else{
		office_l.setVisible(false);
		office.setVisible(false);
		office_s.setVisible(false);
	}
}
/* [end] */



/* [start]	attempts to register a user;
			if successful, redirects to the login page 
			notifies the user, otherwise */
void register(){
	if (user_type.value.equals("College") && isValid()){
		if (new CollegeUserPeer().createNewCollegeUser(username.value, password1.value, new CollegePeer().getCollegeId(office.value)) != null){
			alert("Registration Successful");
			Executions.sendRedirect("index.zul");
		}
		else{
			alert("Registration Failed");
		}
	}
	else if (user_type.value.equals("Unit") && isValid()){
		if (new UnitUserPeer().createNewUnitUser(username.value, password1.value, new UnitPeer().getUnitId(office.value)) != null){
			alert("Registration Successful");
			Executions.sendRedirect("index.zul");
		}
		else{
			alert("Registration Failed");
		}
	}
	else if (user_type.value.equals("Registrar") && isValid()){
		if (new RegistrarUserPeer().createNewRegistrarUser(username.value, password1.value) != null){
			alert("Registration Successful");
			Executions.sendRedirect("index.zul");
		}
		else{
			alert("Registration Failed");
		}
	}
}
/* [end] */



/* [start]	validates the information entered by the user */
boolean isValid(){
	if (username.value.length() < 8)
		return false;
	if (password1.value.length() < 8)
		return false;
	if (!password1.value.equals(password2.value))
		return false;
	if (user_type.equals(_college) && office.value.length == 0)
		return false;
	if (user_type.equals(_unit) && office.value.length == 0)
		return false;
	return true;
}
/* [end] */



/* [start]	filters the contents of the listbox that matches what the user had entered */
void filterItem(String string){
	ListIterator i = data.listIterator();
	List list = new java.util.Vector();
	while(i.hasNext()){
		String temp = i.next(); 
		if(temp.toUpperCase().startsWith(string.toUpperCase())){
			list.add(temp);
		}
	}
	office_lb.getModel().clear();
	office_lb.getModel().addAll(list);
}
/* [end] */